

clear
set more 1
set type double

capture log using "log/phi_euro.log", replace
*****************************************************************
*NOTES:
*split phi_xeu into euro vs noneuro currency
*does not infer missing countries with average firms, but instead creates additional phi_xeu_missing
*
*1. use currency import data to split between xeuro and other
*combine imports by currency data and split xeuro imports into euro currency and other.
*currency17_18 also includes euro trade so drop that

*2. we do not fill in missing currency, but instead construct additional variable of missing curency imports, which we include as a control.
*
*3. combine mvalue_xeu and currency split data with wagebill and inputs data.
*construct share of imports in tvc by year and then take the average
*construct an over phi indicator for firms that have all obs above 1 and drop these later
*
*4. combine data with exports
*******************************************************************************************

*1. construct imports by currency
**********************************************
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop *X
	rename valueI value
	rename country cty_code
	drop if missing(value)
    	drop if missing(currency)
    	
    	gen euro_dum= currency=="EUR"
    	gen dollar_dum=currency=="USD"
    	
    	gen value_euro=value*euro_dum
    	gen value_other=value*(1-euro_dum)
  	gen value_dollar=value*dollar_dum
  
    	collapse (sum) value*, by(vat cty_code )
    	
    	gen share_euro_fc=value_euro/value
    	gen share_other_fc=value_other/value
    	gen share_dollar_fc=value_dollar/value
    	          	    	
        label var share_euro_fc "share of imports in 2017-20 in euro by country"
        label var share_dollar_fc "share of imports in 2017-20 in euro by country"
    save ../data/Mcurrency_vat, replace   
  
 
**********************************************
*2. Construct phi
**********************************
*Construct phi variable as average for 2012-2018
****************************************************
****************************************

*i. construct extra-eurozone imports by firm
*********************************************
use ../../rawdata/M_2019, clear
	append using ../../rawdata/M_2018
	append using ../../rawdata/M_2017
	append using ../../rawdata/M_2016
	append using ../../rawdata/M_2015
	append using ../../rawdata/M_2014
	append using ../../rawdata/M_2013
	append using ../../rawdata/M_2012
	
	drop if land=="EU"

collapse (sum) value, by(vat land month year)
		
	gen eurozone = inlist(land, "AT", "CY", "EE", "FI", "FR", "DE", "GR", "IE", "IT") 
	replace eurozone =1 if inlist(land, "LV", "LT", "LU", "MT", "NL", "PT", "SK", "SI", "ES")
	replace eurozone = 1 if inlist(land, "AD", "MC", "SM", "XK", "ME") 
	rename land cty_code
	
	gen value_eu=value*eurozone
	gen value_xeu=value*(1-eurozone)
	
preserve
	collapse (sum) value*, by(vat)
	save temp/mvalue_vat, replace
restore
preserve
	keep if year==2017|year==2018|year==2019
	collapse (sum) value*, by(vat)
	save temp/mvalue_vat_2019, replace
restore	
	merge m:1 vat cty_code using ../data/Mcurrency_vat, keep(match master)
		
	tab _merge
	rename _merge merge
	
	gen mvalue_xeu_missing=value_xeu if missing(share_euro_fc)|missing(share_other_fc)
		
*we want to split value_xeu by share in euro
	gen mvalue_xeu_euro=value_xeu*share_euro_fc
	gen mvalue_xeu_other=value_xeu*share_other_fc
	gen mvalue_xeu_dollar=value_xeu*share_dollar_fc
	gen mvalue_xeu_nodollar=mvalue_xeu_other-mvalue_xeu_dollar
	gen mvalue_xeu=value_xeu
	gen mvalue_eu=value_eu
	
	collapse (sum) mvalue_*, by(vat year) 
	*drop if mvalue_xeu==0
	gen sh_missing_mcurr=mvalue_xeu_missing/mvalue_xeu
	
save temp/xeuro_imports2012to2019_currency, replace 


*ii. 45% of import xeu data with currency has no corresponding input data
******************************************************************************
	use temp/xeuro_imports2012to2019_currency, clear
		merge 1:1 vat year using temp/tc_allyears, keep(match) nogen
		gen phi_xeu=mvalue_xeu/(wagebill+inputs)
		gen phi_xeu_euro=mvalue_xeu_euro/(wagebill+inputs)
		gen phi_xeu_other=mvalue_xeu_other/(wagebill+inputs)
		gen phi_xeu_missing=mvalue_xeu_missing/(wagebill+inputs)
		gen phi_eu=mvalue_eu/(wagebill + inputs)
		gen phi=phi_xeu + phi_eu
		gen phi_xeu_dollar=mvalue_xeu_dollar/(wagebill+inputs)
		gen phi_xeu_nodollar=mvalue_xeu_nodollar/(wagebill+inputs)
		
		gen over=phi_xeu>=1 & !missing(phi_xeu)
	preserve
		gen x=1
		bys vat (year): egen sumx=sum(x)
		bys vat (year): egen sumover=sum(over)
		gen drop=sumx==sumover
		keep if drop==1
		keep vat
		duplicates drop
		save temp/overphi_vat, replace
	restore
		drop if over==1
				
		keep vat year phi* sh_missing_mcurr
	save temp/imports_tc_allyears_currency, replace
	
		collapse (mean)  mphi_xeu_nodollar=phi_xeu_nodollar mphi_xeu_dollar=phi_xeu_dollar mphi=phi mphi_eu=phi_eu mphi_xeu_euro=phi_xeu_euro mphi_xeu_other=phi_xeu_other mphi_xeu=phi_xeu mphi_xeu_missing=phi_xeu_missing sh_missing_mcurr, by(vat)
		label var mphi_xeu_euro "average of phi_xeu_euro between 2012 and 2019"
	save temp/mphi_xeu_currency, replace	
	
	use temp/imports_tc_allyears_currency
		keep if year==2017|year==2018|year==2019
		collapse (mean)  mphi_xeu_nodollar=phi_xeu_nodollar mphi_xeu_dollar=phi_xeu_dollar mphi=phi mphi_eu=phi_eu mphi_xeu_euro=phi_xeu_euro mphi_xeu_other=phi_xeu_other mphi_xeu=phi_xeu mphi_xeu_missing=phi_xeu_missing sh_missing_mcurr, by(vat)
		label var mphi_xeu_euro "average of phi_xeu_euro between 2017 and 2019"	
						
		save temp/phi_xeu_currency2019, replace	


*3. Combine with exporter annual data
***********************************
*make list of vat for which we have currency data
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop if missing(currency)
	drop *I
	drop if missing(valueX)
	keep vat
	duplicates drop
	save temp/xcurrency_vat, replace

use ../../rawdata/X_2020, clear	
	append using ../../rawdata/X_2019
	append using ../../rawdata/X_2018
	append using ../../rawdata/X_2017
	append using ../../rawdata/X_2016
	append using ../../rawdata/X_2015
	append using ../../rawdata/X_2014
	append using ../../rawdata/X_2013
	append using ../../rawdata/X_2012

*clean data
	drop if land=="EU"
	tostring vat, gen(vat_string)
	drop if substr(vat_string,1,2)=="99"
	drop if missing(vat)
	
	collapse (sum) xvalue_tot=value, by(vat)
*start with all exporters (38,746)	
save temp/temp, replace

*only keep exporters with currency info (22,004)
	merge 1:1 vat using temp/xcurrency_vat, keep(match) nogen

*merge on phi (10,034)
	merge 1:1 vat using temp/mphi_xeu_currency, keep(match master) nogen
	
*work out which obs to drop and which are mphi zero.
*only 25 firms merge on that had overphi=1, but these get dropped because missing mphi_xeu and positive imports
	merge 1:1 vat using temp/mvalue_vat, keep(match master) nogen
	replace mphi_xeu=0 if missing(value_xeu)|value_xeu==0
	replace mphi_xeu_euro=0 if mphi_xeu==0
	replace mphi_xeu_other=0 if mphi_xeu==0
	replace mphi_xeu_missing=0 if mphi_xeu==0
	replace mphi_xeu_dollar=0 if missing(mphi_xeu_dollar)
	replace mphi_xeu_nodollar=0 if missing(mphi_xeu_nodollar)
		
	replace mphi_eu=0 if missing(value_eu)|value_eu==0
	replace mphi=0 if missing(value)|value==0
	
*drop all obs for which mphi_xeu is missing but value_xeu>0
	drop if missing(mphi_xeu) & value_xeu~=0
	
	keep vat mphi* sh_missing
	replace sh_missing_mcurr=0 if mphi_xeu==0
save ../data/mphi_xeu_currency, replace	
	
	
***********************************

*4. combine with monthly exporter data for currency choice regs	
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop *I
	rename valueX value
	drop if missing(value)
	rename country cty_code
	drop if missing(currency)
	drop if cty_code=="EU"
	
*work out which obs to drop and which are phi zero.
	collapse (sum) xvalue_tot=value, by(vat)
	merge 1:1 vat using temp/phi_xeu_currency2019
	merge 1:1 vat using temp/mvalue_vat_2019, keep(match master) nogen
	
	replace mphi_xeu=0 if missing(value_xeu)|value_xeu==0
	replace mphi_xeu_euro=0 if mphi_xeu==0
	replace mphi_xeu_other=0 if mphi_xeu==0
	replace mphi_xeu_missing=0 if mphi_xeu==0
	replace mphi_xeu_dollar=0 if missing(mphi_xeu_dollar)
	replace mphi_xeu_nodollar=0 if missing(mphi_xeu_nodollar)
	
	replace mphi_eu=0 if missing(value_eu)|value_eu==0
	replace mphi=0 if missing(value)|value==0
	
*drop all obs for which phi_xeu is missing but value_xeu>0
	drop if missing(mphi_xeu) & value_xeu~=0
	
	keep vat mphi* sh_missing
	replace sh_missing_mcurr=0 if mphi_xeu==0
	
save ../data/phi_xeu_currency_2019, replace		
	
	
	
	
